Skip to content

Fix quadratic balance top cache rebuild - #6655

Open
ZepsiZola wants to merge 1 commit into
EssentialsX:2.xfrom
ZepsiZola:perf/balance-top-optimisation
Open

ZepsiZola wants to merge 1 commit into
EssentialsX:2.xfrom
ZepsiZola:perf/balance-top-optimisation

Conversation

@ZepsiZola

Copy link
Copy Markdown
Contributor

Information

There was no issue created for this as far as I can tell. I simply just decided to fix it when I noticed the issue in my spark memory allocation report. Basically the issue was that calculateBalanceTopMap allocated a disproportionate amount of memory whenever it refreshed and I discovered it's because it has O(n²) time-complexity and my server has had 300,000 total players.

Details

Proposed fix:

calculateBalanceTopMap collected entries in a LinkedList and then copied the sorted entries into the cache with indexed get(i) calls. Each get(i) on a LinkedList is O(n), so with the default unlimited baltop-entry-limit the copy was O(n²) in the number of users. Plugins that refresh the cache periodically (such as the PlaceholderAPI expansion, every 5 seconds by default) repeat this cost on every refresh.

This PR collects entries in an ArrayList and copies them with an iterator, making the copy O(n) and the whole rebuild O(n log n), bounded by the sort. The sort, tie ordering, entry limit handling and cached data are unchanged, and there are no API or config changes.

The sorting and limiting logic is moved into a package-private sortEntries helper so it can be unit tested. BalanceTopImplTest covers ordering, stable ordering of equal balances, entry limits (including -1, 0 and other negative values, which still produce an empty map as before), empty input, and a 200,000-entry LinkedList that must finish within 5 seconds. With the old indexed get(i) loop restored, that last test times out.

Environments tested:

OS: Fedora Linux 43 (kernel 7.3.6)

Java version: OpenJDK 25.0.3 (Temurin-25.0.3+9)

  • Most recent Paper version (26.3 Paper-273)
  • CraftBukkit/Spigot/Paper 1.12.2
  • CraftBukkit 1.8.8
  • Unit tests (./gradlew build, all tests and checkstyle pass)

Demonstration:

Synthetic benchmark of the sort-and-copy step, old (LinkedList + get(i)) vs new (ArrayList + iterator), random balances, average of several runs after warm-up. Both produce identical ordering at every size.

Users Before After
1,000 1.0 ms 0.4 ms
10,000 33 ms 2.2 ms
50,000 789 ms 19 ms
100,000 3,094 ms 46 ms

Benchmark source: https://gist.github.com/ZepsiZola/18096336efd382bcf44f70de74672a09

calculateBalanceTopMap collected entries in a LinkedList and then copied the
sorted entries into the cache with indexed get(i) calls. Each get(i) on a
LinkedList is O(n), so with the default unlimited baltop-entry-limit the copy
was O(n^2) in the number of users. Plugins that refresh the cache
periodically (such as the PlaceholderAPI expansion, every 5 seconds by
default) repeat this cost on every refresh.

Collect entries in an ArrayList and copy them with an iterator, making the
copy O(n) and the whole rebuild O(n log n), bounded by the sort. The sort,
tie ordering, entry limit handling and cached data are unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant